package com.mindshifter.pivotaltracker.services.v3.service
{
	import com.mindshifter.pivotaltracker.services.v3.events.TrackerServiceEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class TrackerService extends EventDispatcher
	{
		public var result:Object;
		public var trakerMethod:String;
		private var trakerConnector:TrackerConnector;
		private var trackerRequest:URLRequest;
		
		public function TrackerService()
		{
			
		}
		public function connect(serviceUrl:String,trackerVariables:URLVariables = null,trakerUrlHeaders:URLRequestHeader = null,method:String=URLRequestMethod.POST):void {
			trackerRequest = new URLRequest();
			trakerConnector = new TrackerConnector();
			addServiceListeners(trakerConnector);
			trackerRequest.url = serviceUrl;
			trackerRequest.method = method;			
			if(trackerVariables!=null){
				trackerRequest.data = trackerVariables;
			}
			if(trakerUrlHeaders!=null){
				trackerRequest.requestHeaders = [ trakerUrlHeaders ];
			}			
			trakerConnector.load(trackerRequest);
		}
		private function addServiceListeners(dispatcher:IEventDispatcher):void {
			dispatcher.addEventListener(Event.COMPLETE, serviceCompleteHandler);
			dispatcher.addEventListener(Event.OPEN,serviceOpenConnection);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, serviceProgressHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, serviceIoErrorHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,serviceSecurityErrorHandler);
		}
		private function removeServiceListeners(dispatcher:IEventDispatcher):void {
			dispatcher.removeEventListener(Event.COMPLETE, serviceCompleteHandler);
			dispatcher.removeEventListener(Event.OPEN,serviceOpenConnection);
			dispatcher.removeEventListener(ProgressEvent.PROGRESS, serviceProgressHandler);
			dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, serviceIoErrorHandler);
			dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, serviceSecurityErrorHandler);
		}
		
		private function serviceCompleteHandler(event:Event):void {
			removeServiceListeners(trakerConnector);
			dispatchEvent(new TrackerServiceEvent(TrackerServiceEvent.SERVICE_CONNECTED, event.target.data));
			result = event.target.data;
		}
		private function serviceOpenConnection(event:Event):void {
			trace("serviceOpenConnection");
		}
		
		private function serviceProgressHandler(event:ProgressEvent):void {
			trace("serviceProgressHandler");
		}
		
		private function serviceIoErrorHandler(event:IOErrorEvent):void {		
			trace("serviceIoErrorHandler");
			removeServiceListeners(trakerConnector);
			dispatchEvent(new TrackerServiceEvent(TrackerServiceEvent.SERVICE_CONNECTION_ERROR));
		}
		
		private function serviceSecurityErrorHandler(event:SecurityErrorEvent):void {
			trace("serviceSecurityErrorHandler");
			removeServiceListeners(trakerConnector);
			dispatchEvent(new TrackerServiceEvent(TrackerServiceEvent.SERVICE_CONNECTION_ERROR));
		}
	}
}